package com.example.buyticketlishi.view.ac;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.baselibrary.Base.BaseActivity;
import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.RetrufitHelp.BaseSubscriber;
import com.example.buyticketlishi.RetrufitHelp.ExceptionHandle;
import com.example.buyticketlishi.RetrufitHelp.RetrofitClient;
import com.example.buyticketlishi.bean.LSVemPayOrderRequestBean;
import com.example.buyticketlishi.bean.LSVemPayOrderResultBean;
import com.example.buyticketlishi.utils.PeterTimeCountRefresh;
import com.example.buyticketlishi.utils.PeterTimeCountSecondRefresh;

public class PayMoneyActivity extends BaseActivity {

    private String orderBatchNo;
    private String paymentTypeId;
    private String allMoney;

    private LSVemPayOrderRequestBean payOrderRequestBean;
    private String qrCode;
    private EditText etScan;
    private String barcode = "";
    private static final int PROCESSOR_QRCODE = 10;
    private String string;

    private Handler mMyHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PROCESSOR_QRCODE:
                    Bundle data = msg.getData();
                    qrCode = data.getString("PROCESSOR_QRCODE_DATE");

                        payOrderRequestBean.setAuthPayCode(qrCode);
                        goToPay();

                    break;


            }

        }
    };
    private ImageView ivBg;
    private TextView payTime;
    private TextView payMoney;
    private Button backHome;
    private Button backUp;
    private PeterTimeCountRefresh timer;
    private PeterTimeCountSecondRefresh timerSecond;
    private LinearLayout llTime;
    private LinearLayout llBg;
    private LinearLayout llPayOver;
    private TextView tvOrderId;
    private TextView tvOrderMoney;
    private TextView tvOverTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getView() {
        return R.layout.activity_pay_money;
    }

    @Override
    public void init() {

        Intent intent = getIntent();
        orderBatchNo = intent.getStringExtra("orderBatchNo");
        paymentTypeId = intent.getStringExtra("paymentTypeId");
        allMoney = intent.getStringExtra("allMoney");
        etScan = findViewById(R.id.et_scan);

        payTime = findViewById(R.id.tv_pay_time);
        payMoney = findViewById(R.id.tv_pay_money);
        backHome = findViewById(R.id.bt_back_home);
        backUp = findViewById(R.id.bt_begin_back);

        ivBg = findViewById(R.id.iv_pay_money_bg);

        //隐藏的抬头
        llTime = findViewById(R.id.ll_money_time_show);
        llBg = findViewById(R.id.ll_pay_money_bg);
        backHome = findViewById(R.id.bt_back_home);
        backUp = findViewById(R.id.bt_begin_back);

        //支付成功的页面
        llPayOver = findViewById(R.id.ll_money_pay_over);
        tvOrderId = findViewById(R.id.tv_money_order_id);
        tvOrderMoney = findViewById(R.id.tv_money_order_money);
        tvOverTime = findViewById(R.id.tv_money_order_over_time);


        faceInFirst();
//        changeFace(new LSVemPayOrderResultBean());
        if (paymentTypeId != null) {
            ivBg.setVisibility(View.VISIBLE);
            if (paymentTypeId.equals("2468L")){
                // 2468 是微信
                ivBg.setBackgroundResource(R.drawable.wechatbg);
            }else {
                ivBg.setBackgroundResource(R.drawable.alipaybg);
            }
        }else {
            changeFace();
            ivBg.setVisibility(View.GONE);
        }


        if (payOrderRequestBean == null) {
            payOrderRequestBean = new LSVemPayOrderRequestBean();
        }

        payOrderRequestBean.setOrderBatchNo(orderBatchNo);
        payOrderRequestBean.setPrintTicketMode(2);
        payOrderRequestBean.setPaymentTypeId(paymentTypeId);

        payMoney.setText("支付 ¥" + allMoney);

        timer = new PeterTimeCountRefresh(1000 * 300, 1000, payTime);
        timer.start();
        timer.setTimeOverListener(new PeterTimeCountRefresh.TimeOverListener() {
            @Override
            public void timeOver() {
                //倒计时结束，结束页面
                startActivity(new Intent(PayMoneyActivity.this,MainActivity.class));
            }
        });

        doThing();

    }

    private void doThing() {

        backHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PayMoneyActivity.this,MainActivity.class));
            }
        });

        backUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void goToPay() {

        RetrofitClient.getInstance(PayMoneyActivity.this, Base_URL, headers).LiShivemPayOrder(payOrderRequestBean, new BaseSubscriber<LSVemPayOrderResultBean>(PayMoneyActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                SelfToastUtil.getInstance().show(e.message);

            }

            @Override
            public void onNext(LSVemPayOrderResultBean lsVemPayOrderResultBean) {
                String code = lsVemPayOrderResultBean.getCode();
                if (code.equals("200")) {
                    changeFace(lsVemPayOrderResultBean);
                    SelfToastUtil.getInstance().show(lsVemPayOrderResultBean.getMessage());
                } else {
                    SelfToastUtil.getInstance().show(lsVemPayOrderResultBean.getMessage());
                }

            }
        });

    }

    private void changeFace(LSVemPayOrderResultBean lsVemPayOrderResultBean) {
        llTime.setVisibility(View.GONE);
        llBg .setVisibility(View.GONE);
        backHome.setVisibility(View.GONE);
        backUp .setVisibility(View.GONE);
        payMoney.setVisibility(View.GONE);

        llPayOver.setVisibility(View.VISIBLE);
        tvOrderId.setVisibility(View.VISIBLE);
        tvOrderMoney.setVisibility(View.VISIBLE);
        tvOverTime.setVisibility(View.VISIBLE);

        tvOrderId.setText("订单号：" + lsVemPayOrderResultBean.getData().get(0).getCertificateNo());
        tvOrderMoney.setText("订单总金额：" + lsVemPayOrderResultBean.getData().get(0).getSalePriceSum());

        timerSecond = new PeterTimeCountSecondRefresh(1000 * 5, 1000, tvOverTime);
        timerSecond.start();
        timerSecond.setTimeOverListener(new PeterTimeCountSecondRefresh.TimeOverListener() {
            @Override
            public void timeOver() {
                //倒计时结束，结束页面
                startActivity(new Intent(PayMoneyActivity.this,MainActivity.class));
            }
        });
    }

    private void changeFace() {
        llTime.setVisibility(View.GONE);
        llBg .setVisibility(View.GONE);
        backHome.setVisibility(View.GONE);
        backUp .setVisibility(View.GONE);
        payMoney.setVisibility(View.GONE);

        llPayOver.setVisibility(View.VISIBLE);
        tvOrderId.setVisibility(View.VISIBLE);
        tvOrderMoney.setVisibility(View.VISIBLE);
        tvOverTime.setVisibility(View.VISIBLE);

        tvOrderId.setText("订单号：" + orderBatchNo);
        tvOrderMoney.setText("订单总金额：" + allMoney);

        timerSecond = new PeterTimeCountSecondRefresh(1000 * 5, 1000, tvOverTime);
        timerSecond.start();
        timerSecond.setTimeOverListener(new PeterTimeCountSecondRefresh.TimeOverListener() {
            @Override
            public void timeOver() {
                //倒计时结束，结束页面
                startActivity(new Intent(PayMoneyActivity.this,MainActivity.class));
            }
        });
    }

    private void faceInFirst() {
        llTime.setVisibility(View.VISIBLE);
        llBg .setVisibility(View.VISIBLE);
        backHome.setVisibility(View.VISIBLE);
        backUp .setVisibility(View.VISIBLE);
        payMoney.setVisibility(View.VISIBLE);

        llPayOver.setVisibility(View.GONE);
        tvOrderId.setVisibility(View.GONE);
        tvOrderMoney.setVisibility(View.GONE);
        tvOverTime.setVisibility(View.GONE);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            char pressedKey = (char) event.getUnicodeChar();
            barcode += pressedKey;
        }
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            if (barcode != null && !barcode.equals("")) {
                // 那么就去请求服务器
//                SelfToastUtil.getInstance(AnhuiActivity.this).show(etScan.getText().toString());
                Message message = Message.obtain();
                message.what = PROCESSOR_QRCODE;
                Bundle bundle = new Bundle();
                string = etScan.getText().toString();
                bundle.putString("PROCESSOR_QRCODE_DATE", string.trim());
                message.setData(bundle);
                mMyHandler.sendMessage(message);
                etScan.setText("");
                barcode = "";
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }

        if (timerSecond != null) {
            timerSecond.cancel();
        }
    }


}
